package com.dpiinc.TechPage.pen.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.dpiinc.TechPage.R;

import org.jetbrains.annotations.Nullable;

/**
 * Created by hu489_000 on 2018-07-23.
 */

public class InputDialog extends BaseFloatDialog {

    public interface OnClickEditListener {
        public void onClickEdit(String text);
    }

    private static InputDialog mOneButtonDialog;

    private Context mContext;

    public InputDialog(@Nullable Context context) {
        super(context);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_input_layout);
    }

    public static void ShowDialog(@Nullable Context context, String title,String content, final OnClickEditListener mOnClickEditListener) {
        mOneButtonDialog = new InputDialog(context);
        mOneButtonDialog.show();
        TextView btnOK = (TextView) mOneButtonDialog.findViewById(R.id.btn_ok);
        final EditText label_edit = (EditText) mOneButtonDialog.findViewById(R.id.label_edit);
        label_edit.setText(content);
        if(!TextUtils.isEmpty(content)){
            label_edit.setSelection(content.length());
        }
        btnOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mOnClickEditListener != null) {

                    mOnClickEditListener.onClickEdit(label_edit.getText().toString());
                }

                dismissDialog();
            }
        });
        TextView btn_cancel = (TextView) mOneButtonDialog.findViewById(R.id.btn_cancel);
        TextView dialog_title = (TextView) mOneButtonDialog.findViewById(R.id.dialog_title);
        if (!TextUtils.isEmpty(title)) {
            dialog_title.setText(title);
        }

        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismissDialog();
            }
        });

    }

    public static void dismissDialog() {

        if (mOneButtonDialog != null && mOneButtonDialog.isShowing()) {
            mOneButtonDialog.dismiss();
        }

    }
}
